home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14468 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  74 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: help:what is wrong with this code?
  5. Message-ID: <1996Apr15.005244.14555@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <4krmdu$t3h@brahms.udel.edu>
  11. Date: Mon, 15 Apr 1996 00:52:44 GMT
  12.  
  13. In article <4krmdu$t3h@brahms.udel.edu>, yuehong@brahms.udel.edu (Yue-hong Zheng) writes:
  14. >#include <stdio.h>
  15. >
  16. >void quicksort(int[],int,int );
  17. >
  18. >void swap(int*,int*);
  19. >
  20. >main () {
  21. >int k;
  22. >int a[]={1,3,4,2,43,23,5,6,87,92,21};
  23. >quicksort(a,0,10);
  24. >for (k=0;k<=10;k++) {
  25. >printf("%d\n",a[k]);
  26. >}
  27. >return 0;
  28. >}
  29. >
  30. >
  31. >void swap( int* s,int* t) {
  32. >int tmp;
  33. >tmp=*s;
  34. >*s=*t;
  35. >*t=tmp;
  36. >}
  37. >
  38. >void quicksort(int array[],int left,int right) {
  39. >int i,j,median;
  40. >if (right<=left) return ;
  41. >median=(right+left)/2;
  42. >swap(&array[median],&array[right]);
  43. >i=left;
  44. >j=right-1;
  45. >while((j-i)>=-1){
  46. >while((array[i]<array[right])&&(i<=(right-1)))
  47. >i++;
  48. >while((array[j]>array[right])&&(j>=0))
  49. >j--;
  50.  
  51. If you look at the value of 'j' right here in the code, you will see
  52. that it becomes equal to -1 for some steps.  Hence, the swap in the
  53. next line is incorrect, as it accesses memory outside the array. Try
  54. fixing that and proceeding with the next step in figuring out the
  55. error.
  56.  
  57. One other minor nitpick: your indentation (or lack thereof really)
  58. makes it very tough to see what is going on. I had to re-indent the
  59. code to follow it more cleanly.
  60.  
  61. >swap(&array[i],&array[j]);
  62. >}
  63. >swap(&array[i],&array[right]);
  64. >quicksort(array,left,(i-1));
  65. >quicksort(array,(i+1),right);
  66. >}
  67.  
  68.  
  69. -- 
  70. -------------------------------------------------------------------------
  71. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  72. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  73. -------------------------------------------------------------------------
  74.